﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;


namespace SCCM_Client_Injector_2014
{
    internal class LogFile
    {
        public static void LogEvent(string ClientName, int ClientID, string PkgName, string PkgID, string EventType, string Details)
        {
            if (OptionsForm.boolLoggingEnabled == true) { WriteLogEvent(CreateLogEvent(ClientName, ClientID, PkgID, PkgName, EventType, Details));}
            else if (OptionsForm.boolLoggingEnabled == false) { }
        }

        private static string CreateLogEvent(string ClientName, int ClientID, string PkgID, string PkgName, string EventType, string Details)
        {
            string LogEvent = "";

            if (OptionsForm.boolLogTimeStamp == true) { LogEvent = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ","; }
            else { LogEvent = ","; }
            LogEvent = LogEvent + EventType + ",";
            if (OptionsForm.boolLogClientName == true) { LogEvent = LogEvent + ClientName + ","; }
            else { LogEvent = LogEvent + ","; }
            if (OptionsForm.boolLogClientID == true) { LogEvent = LogEvent + ClientID + ","; }
            else { LogEvent = LogEvent + ","; }
            if (OptionsForm.boolLogPkgName == true) { LogEvent = LogEvent + PkgName + ","; }
            else { LogEvent = LogEvent + ","; }
            if (OptionsForm.boolLogPkgID == true) { LogEvent = LogEvent + PkgID + ","; }
            else { LogEvent = LogEvent + ","; }
            LogEvent = LogEvent + Details;
            return LogEvent;
        }

        private static void WriteLogEvent(string LogEvent)
        {
            
            if (File.Exists(Properties.Settings.Default.LogFilePath) == true)
            {
                using (StreamWriter writer = File.AppendText(Properties.Settings.Default.LogFilePath))
                {
                    writer.WriteLine(LogEvent);
                }
            }
            if (File.Exists(Properties.Settings.Default.LogFilePath) == false)
            {
                using (StreamWriter writer = File.AppendText(Properties.Settings.Default.LogFilePath))
                {
                    //System.Deployment.Application.ApplicationDeployment cd = System.Deployment.Application.ApplicationDeployment.CurrentDeployment;
                    writer.WriteLine("***");
                    //writer.WriteLine("***SCCM Client Injector 2014 version 3.2 build" + cd.CurrentVersion.Revision + " (beta) Log File");
                    writer.WriteLine("***SCCM Client Injector 2014 version 3.2 build" + " (beta) Log File");
                    writer.WriteLine("***Created: " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " - CSV format");
                    writer.WriteLine("***,TimeStamp,EventType,ClientName,ClientID,PackageName,PackageID,EventDetails");
                    writer.WriteLine("***");
                    writer.WriteLine(LogEvent);
                }
            }
        }
    }
}
